package com.yunhe.portal.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.constant.PortalConstant;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.filter.ServerContextHolder;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.util.StringUtil;
import com.yunhe.portal.domain.DataNewsInfo;
import com.yunhe.portal.domain.DataNoticeInfo;
import com.yunhe.portal.model.DataNewsInfoVo;
import com.yunhe.portal.model.NewsInfoQueryCondition;
import com.yunhe.portal.model.PhoneQueryCondition;
import com.yunhe.portal.model.PublishStatus;
import com.yunhe.portal.repository.DataNewsInfoRepository;
import com.yunhe.portal.service.DataNewsInfoService;
import com.yunhe.portal.util.MyHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @description 新闻表
 * @author misty
 * @date 2021-10-19
 */
@Service
public class DataNewsInfoServiceImpl implements DataNewsInfoService {

    @Autowired
    private DataNewsInfoRepository dataNewsInfoRepository;

    @Autowired
    private MyHeaderUtil myHeaderUtil;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDataNewsInfo (DataNewsInfo dataNewsInfo) throws ArgumentErrorException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户信息已过期，请重新登录！");
        }
        dataNewsInfo.setPublishPerson(headerUser);
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(StringUtil.isEmpty(projectCode)){
            throw new ArgumentErrorException("请选择对应项目进行维护新闻！");
        }
        dataNewsInfo.setStationId(Long.valueOf(projectCode));
        if (StringUtil.isEmpty(dataNewsInfo.getStatus())
                || PortalConstant.Online_Type.Published.value().equals(dataNewsInfo.getStatus())) {
            if(dataNewsInfo.getPublishTime().getTime() <= System.currentTimeMillis()){
                dataNewsInfo.setStatus(PortalConstant.Online_Type.Published.value());
            }else if(!PortalConstant.Online_Type.Drafts.value().equals(dataNewsInfo.getStatus())){
                dataNewsInfo.setStatus(PortalConstant.Online_Type.ToBeReleased.value());
            }
        }
        if(PortalConstant.Online_Type.Published.value().equals(dataNewsInfo.getStatus())){
            if(PortalConstant.Top_Type.Top.value().equals(dataNewsInfo.getIsTop())){
                dataNewsInfo.setTopTime(new Date());
            }
        }else {
            dataNewsInfo.setIsTop(PortalConstant.Top_Type.NotTop.value());
        }
        int dataNewsInfoSelective = dataNewsInfoRepository.createDataNewsInfoSelective(dataNewsInfo);
        //生成对应的url
        dataNewsInfo.setAppUrl(dataNewsInfo.getAppUrl()+dataNewsInfo.getId());
        dataNewsInfo.setWeChatUrl(dataNewsInfo.getWeChatUrl()+dataNewsInfo.getId());
        dataNewsInfoRepository.updateDataNewsInfoByPrimaryKeySelective(dataNewsInfo);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDataNewsInfo(int id) {
        return dataNewsInfoRepository.deleteDataNewsInfoByPrimaryKey(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDataNewsInfo(DataNewsInfo dataNewsInfo) throws ArgumentErrorException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户信息已过期，请重新登录！");
        }
        dataNewsInfo.setUpdatePerson(headerUser);
        if (StringUtil.isEmpty(dataNewsInfo.getStatus())
                || PortalConstant.Online_Type.Published.value().equals(dataNewsInfo.getStatus())) {
            if(dataNewsInfo.getPublishTime().getTime() <= System.currentTimeMillis()){
                dataNewsInfo.setStatus(PortalConstant.Online_Type.Published.value());
                dataNewsInfo.setPublishPerson(headerUser);
            }else if(!PortalConstant.Online_Type.Drafts.value().equals(dataNewsInfo.getStatus())){
                dataNewsInfo.setStatus(PortalConstant.Online_Type.ToBeReleased.value());
            }
        }
        if(PortalConstant.Online_Type.Published.value().equals(dataNewsInfo.getStatus())){
            if(PortalConstant.Top_Type.Top.value().equals(dataNewsInfo.getIsTop())){
                dataNewsInfo.setTopTime(new Date());
            }
        }else {
            dataNewsInfo.setIsTop(PortalConstant.Top_Type.NotTop.value());
        }
        return dataNewsInfoRepository.updateDataNewsInfoByPrimaryKeySelective(dataNewsInfo);
    }


    @Override
    public DataNewsInfo findDataNewsInfoById(int id) {
        return dataNewsInfoRepository.searchDataNewsInfoByPrimaryKey(id);
    }

    @Override
    public PageResult findDataNewsInfoByCondition(NewsInfoQueryCondition newsInfoQueryCondition) {
        PageResult result = null;
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(!StringUtil.isEmpty(projectCode)){
            newsInfoQueryCondition.setStationId(Long.valueOf(projectCode));
        }
        PageHelper.startPage(newsInfoQueryCondition.getPage(),newsInfoQueryCondition.getSize());
        List<DataNewsInfoVo> dataNewsInfoVos = dataNewsInfoRepository.findDataNewsInfoByCondition(newsInfoQueryCondition);
        if(!CollectionUtils.isEmpty(dataNewsInfoVos)){
            PageInfo<DataNewsInfoVo> pageInfo = new PageInfo<>(dataNewsInfoVos);
            result = new PageResult(newsInfoQueryCondition.getPage(),newsInfoQueryCondition.getSize(), (int) pageInfo.getTotal(),dataNewsInfoVos);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setTop(long id) throws ArgumentErrorException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户信息已过期，请重新登录！");
        }
        DataNewsInfo dataNewsInfo = new DataNewsInfo();
        dataNewsInfo.setId(id);
        dataNewsInfo.setUpdateTime(new Date());
        dataNewsInfo.setIsTop(PortalConstant.Top_Type.Top.value());
        dataNewsInfo.setTopTime(new Date());
        dataNewsInfo.setStatus(PortalConstant.Online_Type.Published.value());
        //dataNewsInfo.setPublishTime(new Date());
        dataNewsInfo.setUpdatePerson(headerUser);

        dataNewsInfoRepository.updateDataNewsInfoByPrimaryKeySelective(dataNewsInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPublishStatus(PublishStatus publishStatus) throws ArgumentErrorException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户信息已过期，请重新登录！");
        }
        List<Long> ids = publishStatus.getIds();
        if(!CollectionUtils.isEmpty(ids)){
            for (Long id : ids) {
                DataNewsInfo dataNewsInfo = new DataNewsInfo();
                dataNewsInfo.setId(id);
                if(PortalConstant.Online_Type.Published.value().equals(publishStatus.getPublishStatus())){
                    DataNewsInfo dataNewsInfo1 = dataNewsInfoRepository.searchDataNewsInfoByPrimaryKey(Math.toIntExact(id));
                    if(dataNewsInfo1.getPublishTime().getTime() <= System.currentTimeMillis() &&
                            PortalConstant.Online_Type.ToBeReleased.value().equals(dataNewsInfo1.getStatus())){
                        dataNewsInfo.setStatus(PortalConstant.Online_Type.Published.value());
                        dataNewsInfo.setPublishPerson(headerUser);
                    }
                    dataNewsInfo.setActivity("1");
                }else if(PortalConstant.Online_Type.Disable.value().equals(publishStatus.getPublishStatus())){
                    dataNewsInfo.setActivity("0");
                }
                dataNewsInfo.setUpdatePerson(headerUser);
                dataNewsInfoRepository.updateDataNewsInfoByPrimaryKeySelective(dataNewsInfo);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publishTimedNews() {
        dataNewsInfoRepository.publishTimedNew();
    }

    @Override
    public PageResult findAppNews(PhoneQueryCondition phoneQueryCondition) {
        PageResult result = null;
        PageHelper.startPage(phoneQueryCondition.getPage(), phoneQueryCondition.getSize());
        List<DataNewsInfoVo> dataNewsInfoVos = dataNewsInfoRepository.findPhonesNewsByCondition(phoneQueryCondition);
        if(!CollectionUtils.isEmpty(dataNewsInfoVos)){
            PageInfo<DataNewsInfoVo> pageInfo = new PageInfo<>(dataNewsInfoVos);
            result = new PageResult(phoneQueryCondition.getPage(), phoneQueryCondition.getSize(), (int) pageInfo.getTotal(),dataNewsInfoVos);
        }
        return result;
    }

    @Override
    public PageResult findWeChatNews(PhoneQueryCondition phoneQueryCondition) {
        PageResult result = null;
        PageHelper.startPage(phoneQueryCondition.getPage(), phoneQueryCondition.getSize());
        List<DataNewsInfoVo> dataNewsInfoVos = dataNewsInfoRepository.findPhonesNewsByCondition(phoneQueryCondition);
        if(!CollectionUtils.isEmpty(dataNewsInfoVos)){
            PageInfo<DataNewsInfoVo> pageInfo = new PageInfo<>(dataNewsInfoVos);
            result = new PageResult(phoneQueryCondition.getPage(), phoneQueryCondition.getSize(), (int) pageInfo.getTotal(),dataNewsInfoVos);
        }
        return result;
    }


}